package com.szholly.pro.watergas_server.water.localcheck;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.utils.spring.SpringBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.sf.json.JSONObject;

/**
 * 质控现场考核
 * @author hyf
 *
 */
@Controller
@RequestMapping(value="localCheck")
public class localCheckController extends BaseController {
	
	@Autowired
	localCheckService localCheckService;
	
	/**
	 * 质控现场考核主表查询
	 * @param resq
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping(value="getList")
	public void getList(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> params = queryStationQueryParams();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("QC_LOCALECHECK");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list=localCheckService.getList(params, pTable, pageInfo);
		JSONObject JSON= DataUtil.listToJSONObject(list, pTable, pageInfo);
		outString(JSON.toString());
	}
	/**
	 * 考核详情
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="getDetail")
	public void getDetail(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> params = queryStationQueryParams();
		List<RowBase> list=localCheckService.getDetail(params);
		JSONObject json=DataUtil.listToJSONObject(list);
		outString(json.toString());
	}
	@RequestMapping(value="toExecl")
	public void toExecl(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> params = queryStationQueryParams();
		//获取主表考核信息
		List<RowBase> list=localCheckService.getList(params, null, null);
		//获取详情考核信息
		List<RowBase> listdetail=localCheckService.getDetail(params);
		HashMap<String, Object> data = new HashMap<>();
		data.put("check", list.get(0));
		data.put("detail", listdetail);
		data.put("checktype", params.get("checktype"));
		HtmlToExcel(data, "qc/localeCheck.html", "考核检查表.xls");
	}
	/**
	 * 获取界面传递的参数，需要重构！！
	 * @return
	 */
	private Map<String,String> queryStationQueryParams() {
		Map<String,String> params = new HashMap<>();
		String start = RequestUnit.getParameter("start");
		String end = RequestUnit.getParameter("end");
		String GUID = RequestUnit.getParameter("GUID");
		String checktype = RequestUnit.getParameter("checktype");
		String STATIONID = RequestUnit.getParameter("STATIONID");
		String CHECK_CONTENT = RequestUnit.getParameter("CHECK_CONTENT");
		if (StringUtil.IsNotNullOrEmpty(STATIONID)) {
			params.put("STATIONID", STATIONID);
		}
		if (StringUtil.IsNotNullOrEmpty(CHECK_CONTENT)) {
			params.put("CHECK_CONTENT", CHECK_CONTENT);
		}
		if (StringUtil.IsNotNullOrEmpty(start)) {
			params.put("start", start);
		}
		if (StringUtil.IsNotNullOrEmpty(end)) {
			params.put("end", end);
		}
		if (StringUtil.IsNotNullOrEmpty(GUID)) {
			params.put("GUID", GUID);
		}
		if (StringUtil.IsNotNullOrEmpty(checktype)) {
			if(!checktype.equals("all")&&checktype!="all"){
				params.put("checktype", checktype);
			}			
		}
		return params;
	}
}
